package com.qnw.CardGroupManagement.adapter;
import android.widget.*;
import android.view.*;
import java.util.*;
import com.qnw.CardGroupManagement.*;
import android.content.*;
import com.ygo.feihua.*;
import android.support.v7.widget.*;
import android.view.View.*;
import com.ygo.feihua.util.*;
import android.util.*;

public class CardListAdapter extends RecyclerView.Adapter<CardListAdapter.ViewHolder>
{


	private List<Card> data;
	private Context context;
	private DialogUtils du;
	private boolean scrollState;
	
	public CardListAdapter(Context context,List<Card> data){
		this.context=context;
		this.data=data;
		scrollState=false;
		du=DialogUtils.getdx(context);
	}

	private OnCardItemClickListener oncard;
	public interface OnCardItemClickListener{
		void onClick(Card card,int posotion);	
	}

		public void setOnCardItemClickListener(OnCardItemClickListener oncar){
			oncard=oncar;
		}
	
	
	
	public void remove(int position)
	{
		data.remove(position);
		notifyDataSetChanged();
		// TODO: Implement this method
	}

	public List<Card> getData(){
		return data;
	}
	
	public void add(Card card)
	{
		data.add(card);
		notifyDataSetChanged();
		// TODO: Implement this method
	}

	public void removeAll()
	{
		data.clear();
		notifyDataSetChanged();
		// TODO: Implement this method
	}

	public void setScrollState(boolean scrollState)
	{
		this.scrollState = scrollState;
		// TODO: Implement this method
	}

	public void sxAll(List<Card> data)
	{
		
		this.data.clear();
		for(Card card:data){
			this.data.add(card);
		}
		//Log.d("CardListAdapter","传入"+data.size()+"当前"+this.data.size());
		notifyDataSetChanged();
		// TODO: Implement this method
	}
	
	public static class ViewHolder extends RecyclerView.ViewHolder{
		TextView card_name,card_atk,card_def,card_id;
		LinearLayout card_lin;
		public ImageView card_kt;
		View v;
		
		public ViewHolder(View v){
			super(v);
			this.v=v;
			card_name=(TextView) v.findViewById(R.id.card_name);
			card_atk=(TextView) v.findViewById(R.id.card_atk);
			card_def=(TextView) v.findViewById(R.id.card_def);
			card_id=(TextView) v.findViewById(R.id.card_id);
			card_lin=(LinearLayout) v.findViewById(R.id.card_lin);
			card_kt=(ImageView) v.findViewById(R.id.card_pics);
		}
	}
	

	@Override
	public CardListAdapter.ViewHolder onCreateViewHolder(ViewGroup p1, int p2)
	{
		View v=LayoutInflater.from(p1.getContext()).inflate(R.layout.item_card_list,p1,false);
		
		
		// TODO: Implement this method
		return new ViewHolder(v);
	}

	@Override
	public void onBindViewHolder(CardListAdapter.ViewHolder vh, final int position)
	{
		final Card card=data.get(position);
		vh.card_name.setText(card.getName());
		vh.card_id.setText(card.getId());
		if(card.getAtk().equals("N/A")){
			vh.card_lin.setVisibility(View.GONE);
		}else{
			vh.card_lin.setVisibility(View.VISIBLE);
			vh.card_atk.setText(card.getAtk());
			vh.card_def.setText(card.getDef());
		}
		if (!scrollState){
			Gongju.setKt(context, vh.card_kt, card.getId());
		}else{
			vh.card_kt.setImageResource(R.drawable.unknown);
		}
		vh.v.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					if(oncard!=null){
						oncard.onClick(card,position);
					}else{
						du.dialogci(false,card.getId());
					}
					
					// TODO: Implement this method
				}
			});
		
		
		// TODO: Implement this method
	}

	@Override
	public int getItemCount()
	{
		// TODO: Implement this method
		return data.size();
	}
}
